#include "wds_bullet_ion.h"
#include "wds_bullet_manager.h"
#include "wds_sounds.h"
#include "wds_lifeobject.h"
#include "wds_enemy_manager.h"
#include "wds_enemy_collection.h"

#include "num/rand.h"


WDSBulletION::WDSBulletION(float Speed, 
				bool Rotate,
				float DamagePower,
				BULLET_TYPES Type) : WDSBullet( Speed,
												Rotate,
												DamagePower,
												Type),
									m_CountToBounce(2),
									m_DelayShowingBounces(-1.0f)
{
	m_bLeaveAlphaTrail = true;
}


void			WDSBulletION::OnHit(WDSLifeObject* pObj)
{
	WDSBullet::OnHit(pObj);
	if(!pObj)
		return;	

	m_CountToBounce--;
	if(m_CountToBounce <= 0)
		return;
		

	const float MIN_DIST_TO_BOUNCE = 0.1f;

	std::vector<WDSLifeObject*>		RandomList;
	
	
	std::vector<WDSEnemyCollection*>	EnemyCollection;
	WDSEnemyManager::Instance().GetEnemyCollection(EnemyCollection);
	math::Vec2 Pos1 = pObj->GetPos();
	math::Movement::ChangeToSquareVec2(Pos1);
	math::Vec2 Pos2;

	for(int i = 0; i < (int)EnemyCollection.size(); i++)
	{
		WDSEnemyCollection* pCol = EnemyCollection[i];
		for(int j = 0; j < (int)pCol->GetCount(); j++)
		{
			WDSEnemy* pEnemy = pCol->GetEnemyByIndex(j);
			if(pEnemy == pObj)
				continue;

			if(!pEnemy->IsAlive())
				continue;
			
			Pos2 = pEnemy->GetPos();
			math::Movement::ChangeToSquareVec2(Pos2);
			
			if((Pos2 - Pos1).Magnitude() <= MIN_DIST_TO_BOUNCE * WDSBulletManager::Instance().GetSkill()->m_IONGunMaster )
			{
				RandomList.push_back(pEnemy);
			}
		}
	}

	float DegreeToShoot = 0; 
	int CurrentBouceCount = 0;
	int Index = 0;
	int ExtraBounce = 0;
	if( WDSBulletManager::Instance().GetSkill()->m_IONGunMaster > 1.0f)
		ExtraBounce = 1;
	while( CurrentBouceCount < (m_CountToBounce + ExtraBounce)  && RandomList.size() > 0)
	{
		Index = num::RAND32::Instance().RandInt(0,RandomList.size()-1);
		WDSLifeObject* pTargetToAim = RandomList[ Index ];
		//math::Vec2 TempDir = pTargetToAim->GetPos() - pObj->GetPos(); 
		//TempDir.y /= GraphicsUtil::W2H;
		//DegreeToShoot =  math::Facing::AngleVector( TempDir );

		m_BounceFrom = pObj->GetPos();
		m_BounceTos.push_back(pTargetToAim->GetPos());
		m_DelayShowingBounces = 1.0f;
		RandomList.erase(RandomList.begin() + Index);
		CurrentBouceCount++;
		
	}
	

    //WDSBullet* pBullet = NULL;
	//pBullet = WDSBulletManager::Instance().Shoot(m_Type,NULL, GetPos(), DegreeToShoot, WDSSounds::SFX_NONE);

	//if(pBullet)
	//{
	//	((WDSBulletION*)pBullet)->SetCountToBounce(m_CountToBounce);
	//	pBullet->SetIgnoreObj(pObj);		// don't hit more than once		

	//	((WDSBulletION*)pBullet)->SetDamagePercentage( ((WDSBulletION*)pBullet)->GetDamagePercentage() * 0.35f );
	//}

}


void	WDSBulletION::ReSpawn(const math::Vec2& Pos,
											WDSBullet::STARTPOS StartDir,
											float Degree
											)
{
	m_DelayShowingBounces = -1.0f;
	m_BounceTos.resize(0);
	WDSBullet::ReSpawn(Pos, StartDir, Degree);
}

void	WDSBulletION::Render(const math::Vec2& Offset,float Scale)
{
	if(m_DelayShowingBounces > 0)
	{
		for(int i = 0; i < (int)m_BounceTos.size(); i++)
		{
			m_Sprite.m_Pos = m_BounceTos[i] - Offset;	
			m_Sprite.m_Color = ColorfRGBA(1.0f,1.0f,1.0f,1.0f * m_DelayShowingBounces);
			m_Sprite.m_Size.x = m_Sprite.m_SizeOriginal.x;
			const math::Vec2 Dir = (m_BounceTos[i] - m_BounceFrom);
			m_Sprite.m_Size.y = Dir.Magnitude() * 2.0f;
			m_Sprite.m_Size *= Scale;
			m_Sprite.m_Rotation = math::Facing::AngleVector(Dir);

			m_Sprite.RenderRotated();
		}

	}

	WDSBullet::Render(Offset, Scale);
}
	
void	WDSBulletION::Tick(float dt,float SkillBulletSpeed, const math::Vec2& Offset)
{
	if(m_DelayShowingBounces > 0)
		m_DelayShowingBounces -= dt;

	WDSBullet::Tick(dt, SkillBulletSpeed, Offset);
}

bool	WDSBulletION::IsCanRespawn() const
{
	if(m_DelayShowingBounces > 0)
		return false;

	return WDSBullet::IsCanRespawn();
}

